package com.service.purchase;

import java.io.IOException;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.common.spring.SpringMvcUtils;
import com.dao.purchase.ContractDao;
import com.dao.purchase.InquiryResultMapper;
import com.dao.purchase.OrderDao;
import com.dao.purchase.PlanMmMapper;
import com.dao.purchase.SupplyDealMapper;
import com.dao.purchase.SupplyDealMmMapper;
import com.dao.purchase.SupplyOfferMapper;
import com.dao.supply.InfoMapper;
import com.dao.sys.SysUserMapper;
import com.enums.NeedTypeEnum;
import com.enums.WorkFlowIdModel;
import com.google.protobuf.ServiceException;
import com.model.external.oa.OaWorkFlowResult;
import com.model.purchase.ContractModel;
import com.model.purchase.OrderModel;
import com.model.purchase.OrderVoModel;
import com.model.purchase.PurchaseInquiryResult;
import com.model.purchase.PurchasePlanMm;
import com.model.purchase.PurchaseSupplyDeal;
import com.model.purchase.PurchaseSupplyDealMm;
import com.model.supply.SupplyInfoModel;
import com.model.sys.SysUser;
import com.service.external.oa.CreateContractService;
import com.service.external.oa.CreateResultService;
import com.service.external.oa.CreateSpecialContractService;
import com.service.external.oa.GetWFStatusService;
import com.service.external.sap.SapPurchaseService;
import com.service.sys.SysDictService;

@Service
public class OrderService {
	static final Logger logger = LoggerFactory.getLogger(OrderService.class);

	@Autowired
	OrderDao orderDao;
	@Autowired
	InquiryResultMapper resultMapper;
	@Autowired
	SupplyOfferMapper supplyOfferMapper;
	@Autowired
	SupplyDealMapper supplyDealMapper;
	@Autowired
	SupplyDealMmMapper supplyDealMmMapper;
	@Autowired
	SapPurchaseService sapPurchaseService;
	@Autowired
	CreateContractService createContractService;
	@Autowired
	CreateSpecialContractService specialContractService;
	@Autowired
	CreateResultService createResultService;
	@Autowired
	GetWFStatusService getWFStatusService;
	@Autowired
	GetWFStatusService wfStatusService;
	@Autowired
	SysDictService sysDictService;
	@Autowired
	CommonService commonService;
	@Autowired
	ContractDao contractDao;
	@Autowired
	PlanMmMapper planMmMapper;
	@Autowired
	SysUserMapper sysUserMapper2;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<OrderModel> list = orderDao.selectList(queryMap);
		int count = orderDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String ordId) {
		HttpResult httpResult = new HttpResult();
		OrderModel orderModel = orderDao.load(ordId);
		OrderVoModel orderVoModel = new OrderVoModel();
		BeanUtils.copyProperties(orderModel, orderVoModel);
		PurchaseSupplyDeal supplyDeal = supplyDealMapper.load(orderModel.getResId(), orderModel.getInfId());
		orderVoModel.setSupplyDeal(supplyDeal);

		List<PurchaseSupplyDeal> supplyDeals = new ArrayList<PurchaseSupplyDeal>();
		supplyDeals.add(supplyDeal);
		orderVoModel.setSupplyDeals(supplyDeals);

		List<PurchaseSupplyDealMm> supplyDealMms = supplyDealMmMapper.loadByInfIdAndResId(orderModel.getResId(),
				orderModel.getInfId());
		orderVoModel.setSupplyDealMms(supplyDealMms);
		httpResult.setData(orderVoModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 退回
	 * 
	 * @param purchasePlanMm
	 * @return
	 */
	@Transactional
	public HttpResult back(OrderModel resultModel) {
		if (resultModel == null) {
			return HttpResult.error("请求参数不能为空!");
		}

		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (StringUtils.isNotNull(resultModel.getOaFlowId() + "")) {
			// oa退回才可撤回
			HttpResult httpResult = wfStatusService.getWorkflowRequestLogs(sysUser.getWxid(),
					WorkFlowIdModel.puchaseContractString, resultModel.getOaFlowId() + "");
			if (httpResult.getCode() != HttpStatus.SUCCESS_CODE) {
				return httpResult;
			}
		}

		HashMap<String, Object> hashMap = new HashMap();
		hashMap.put("ordId", resultModel.getOrdId());
		List<ContractModel> list = contractDao.selectList(hashMap);
		if (list != null && list.size() > 0) {
			return HttpResult.error("请先在合同签署删除合同！");
		}
		int result = 0;

		resultModel.setOaFlowId(0);
		resultModel.setStatus(Global.NO + "");
		result = orderDao.update(resultModel);
		if (result > 0) {
			return HttpResult.ok("回退成功");
		} else {
			return HttpResult.error("不存在此订单！");
		}
	}

	@Autowired
	InfoMapper infoMapper;

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public HttpResult save(OrderVoModel orderModel) throws Exception {
		if (orderModel == null) {
			return HttpResult.error("参数不能为空!");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!StringUtils.isNotNull(sysUser.getSap())) {
			return HttpResult.error("采购组账号不存在！");
		}
		SupplyInfoModel supplyInfoModel = infoMapper.load(orderModel.getInfId());
		orderModel.setOrdLegal(supplyInfoModel.getInfLegalPerson());
		orderModel.setOrdContractName(supplyInfoModel.getInfContactsName());

		result += supplyDealMapper.update(orderModel.getSupplyDeal());

		int i = 0;
		for (PurchaseSupplyDealMm dealMm : orderModel.getSupplyDealMms()) {
			PurchasePlanMm programMm = planMmMapper.load(dealMm.getPlanId());
			dealMm.setInfCode(orderModel.getInfCode());
			dealMm.setInfName(orderModel.getInfName());
			dealMm.setPlanNedType(programMm.getPlanNedType());
			dealMm.setPlanDeptCode(programMm.getPlanDeptCode());
			dealMm.setPlanCorpCode(programMm.getPlanCorpCode());
			dealMm.setPlanCode(programMm.getPlanCode());
			dealMm.setMmCode(programMm.getMmCode());
			dealMm.setMmDetail(programMm.getMmDetail());
			dealMm.setMmUnit(programMm.getMmUnit());
			dealMm.setMmTypeCode(programMm.getMmTypeCode());
			dealMm.setPlanFactoryCode(programMm.getPlanFactoryCode());
			dealMm.setPlanGoodType(programMm.getPlanGoodType());
			dealMm.setPlanProType(programMm.getPlanProType());
			dealMm.setPlanOrderCode(programMm.getPlanOrderCode());
			dealMm.setPlanMeanCode(programMm.getPlanMeanCode());
			dealMm.setPlanCostCode(programMm.getPlanCostCode());
			dealMm.setPlanNeedDate(programMm.getPlanNeedDate());
			dealMm.setPlanAssessType(
					StringUtils.isNullOrEmpty(programMm.getPlanAssessType()) ? "" : programMm.getPlanAssessType());
			dealMm.setMmMain(programMm.getMmMain());
			dealMm.setMmTypeName(programMm.getMmTypeName());
			dealMm.setPlanCostDetail(programMm.getPlanCostDetail());
			dealMm.setPlanFactoryName(programMm.getPlanFactoryName());
			if (dealMm.getDealNum() == null || dealMm.getDealNum().compareTo(BigDecimal.ZERO) == 0) {

			} else {
				dealMm.setMmRowNum((++i) * 10 + "");
			}
			supplyDealMmMapper.update(dealMm);
		}

		// 调用sap生成编码
		if (orderModel.getOrdIsSign().equals(Global.NO + "")) {
			PurchaseSupplyDeal supplyDeal = supplyDealMapper.load(orderModel.getResId(), orderModel.getInfId());
			supplyDeal.setInqGroup(sysUser.getSap());
			if (orderModel.getOrdCode() != null && orderModel.getOrdCode().length() == 16) {
				orderModel.setOrdCode("");// 当是否框架从是变为否时
			}
			HttpResult sapHttpResult = sapPurchaseService.createSapOrder(supplyDeal, orderModel);
			if (sapHttpResult.getCode() == HttpStatus.SUCCESS_CODE) {
				orderModel.setOrdCode(sapHttpResult.getData().toString());
//				result += orderDao.insert(orderModel);
				// 修改sap状态
				supplyDeal.setInqSapStatus(Global.YES + "");
				result += supplyDealMapper.update(supplyDeal);
			} else {
				logger.error(sapHttpResult.getMsg());
				if (sapHttpResult.getMsg().contains("物料")) {
					return HttpResult.error("请联系主数据管理员：" + sapHttpResult.getMsg());
				} else {
					return HttpResult.error("请根据以下错误信息先查找使用手册常见问题：" + sapHttpResult.getMsg());
				}
			}
		} else {
			if (StringUtils.isNullOrEmpty(orderModel.getOrdCode()) || orderModel.getOrdCode().length() != 16) {
				orderModel.setOrdCode(commonService.createCodeSap("purchase_order", "ord_code"));
			}
		}

		if (orderModel.getStatus().equals(Global.YES + "")) {
			// 调用oa接口
//			HashMap<String, Object> dicHashMap = new HashMap<String, Object>();
//			dicHashMap.put("dicName", DictGlobal.oaPurchaseCheckParentNameString);
//			dicHashMap.put("query", DictGlobal.oaPurchaseCheckNameString);
//			List<SysDict> dicts = sysDictService.queryItemByTypeName(dicHashMap);
//			if (dicts != null && dicts.size() > 0) {
//				String valueString = dicts.get(0).getDicValue();
//				BigDecimal bigDecimal = new BigDecimal(valueString);
//				if (bigDecimal.compareTo(orderModel.getOrdFreight()) <= 0) {
//			if (orderModel.getOrdContractType().equals(Global.YES + "")) {
//				HttpResult oaResult = createContractService.CreateContract(orderModel, sysUser.getWxid());
//				if (oaResult.getCode() != HttpStatus.SUCCESS_CODE) {
//					// 强制手动事务回滚
//					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//					return HttpResult.error(oaResult.getMsg());
//				}
//				OaWorkFlowResult oaWorkFlowResult = (OaWorkFlowResult) oaResult.getData();
//				orderModel.setOaFlowId(oaWorkFlowResult.getOaFlowId());
//			}
//				}
//			}
		}
		if (!StringUtils.isNotNull(orderModel.getOrdId())) {
			orderModel.setOrdId(UUID.randomUUID().toString());
			result = orderDao.insert(orderModel);
		} else {
			result = orderDao.update(orderModel);
		}
		if (result > 0) {
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(orderModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 提交
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public HttpResult commit(OrderVoModel orderModel) throws Exception {
		if (orderModel == null) {
			return HttpResult.error("参数不能为空!");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!StringUtils.isNotNull(sysUser.getSap())) {
			return HttpResult.error("采购组账号不存在！");
		}
		SupplyInfoModel supplyInfoModel = infoMapper.load(orderModel.getInfId());
		orderModel.setOrdLegal(supplyInfoModel.getInfLegalPerson());
		orderModel.setOrdContractName(supplyInfoModel.getInfContactsName());

		if (orderModel.getStatus().equals(Global.YES + "")) {
			// 调用oa接口
			if (orderModel.getOrdContractType().equals(Global.YES + "")) {
				HttpResult oaResult = createContractService.CreateContract(orderModel, sysUser.getWxid());
				if (oaResult.getCode() != HttpStatus.SUCCESS_CODE) {
					// 强制手动事务回滚
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return HttpResult.error(oaResult.getMsg());
				}
				OaWorkFlowResult oaWorkFlowResult = (OaWorkFlowResult) oaResult.getData();
				orderModel.setOaFlowId(oaWorkFlowResult.getOaFlowId());
			}
		}

		orderModel.setStatus(Global.YES + "");
		result = orderDao.update(orderModel);
		for (PurchaseSupplyDealMm dealMm : orderModel.getSupplyDealMms()) {
			dealMm.setOrdCode(orderModel.getOrdCode());
			supplyDealMmMapper.update(dealMm);
		}

		if (result > 0) {
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(orderModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	// 退回
	public HttpResult backAdmin(OrderVoModel orderModel) {
		HttpResult httpResult = new HttpResult();
		orderModel.setStatus(Global.NO + "");
		orderDao.update(orderModel);
		return httpResult;
	}

	// 转交
	public HttpResult change(String contractEnd, String ordId) {
		HttpResult httpResult = new HttpResult();
		OrderModel orderModel = orderDao.load(ordId);
		orderModel.setInqContractEnd(contractEnd);
		orderDao.update(orderModel);
		return httpResult;
	}

	// 转交
	public HttpResult transfer(String userId, String ordId) {
		HttpResult httpResult = new HttpResult();
		SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
		OrderModel orderModel = orderDao.load(ordId);
		orderModel.setCreateId(userId);
		orderModel.setCreateName(sysUser.getUsername());
		orderDao.update(orderModel);
		return httpResult;
	}

	// 推送oa
	@Transactional
	public HttpResult sendOa(OrderVoModel orderModel) throws Exception {
		if (orderModel == null) {
			return HttpResult.error("参数不能为空!");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!StringUtils.isNotNull(sysUser.getSap())) {
			return HttpResult.error("采购组账号不存在！");
		}
		SupplyInfoModel supplyInfoModel = infoMapper.load(orderModel.getInfId());
		orderModel.setOrdLegal(supplyInfoModel.getInfLegalPerson());
		orderModel.setOrdContractName(supplyInfoModel.getInfContactsName());

		if (orderModel.getStatus().equals(Global.YES + "")) {
			// 调用oa接口
			if (orderModel.getOrdContractType().equals(Global.YES + "")) {
				HttpResult oaResult = createContractService.CreateContract(orderModel, sysUser.getWxid());
				if (oaResult.getCode() != HttpStatus.SUCCESS_CODE) {
					// 强制手动事务回滚
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return HttpResult.error(oaResult.getMsg());
				}
				OaWorkFlowResult oaWorkFlowResult = (OaWorkFlowResult) oaResult.getData();
				orderModel.setOaFlowId(oaWorkFlowResult.getOaFlowId());
			}
		}

		orderModel.setOrdContractType(Global.YES + "");
		result = orderDao.update(orderModel);

		if (result > 0) {
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(orderModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(orderModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult saveSpecial(OrderVoModel orderVoModel) {
		if (orderVoModel == null || orderVoModel.getSupplyDealMms() == null) {
			return HttpResult.error("参数不能为空");
		}
		SysUser sysUser = SpringMvcUtils.getSysUser();
		HttpResult httpResult = new HttpResult();
		int result = 0;
		PurchaseSupplyDeal supplyDeal = orderVoModel.getSupplyDeal();
		supplyDeal.setInqIsContract("special");
		supplyDeal.setInqId(Global.MINUS_ONE + "");
		supplyDeal.setInqGroup(sysUser.getSap());
		String resIdString = supplyDeal.getResId();
		if (!StringUtils.isNotNull(resIdString)) {
			String idString = UUID.randomUUID().toString();
			orderVoModel.setResId(idString);
			orderVoModel.setOrdId(idString);
			supplyDeal.setResId(idString);
		}

		supplyDeal.setDealIsCheck(Global.YES + "");
		if (supplyDealMapper.update(supplyDeal) == 0) {
			result += supplyDealMapper.insert(supplyDeal);
		} else {
			result++;
		}
		int i = 0;
		for (PurchaseSupplyDealMm supplyDealMm : orderVoModel.getSupplyDealMms()) {
			supplyDealMm.setInfId(orderVoModel.getInfId());
			supplyDealMm.setInfCode(orderVoModel.getInfCode());
			supplyDealMm.setInfName(orderVoModel.getInfName());
			supplyDealMm.setResId(orderVoModel.getResId());
			supplyDealMm.setPlanDeptCode("1000");
			supplyDealMm.setPlanId("sepcial");
			supplyDealMm.setPlanCode("");
			supplyDealMm.setMmCode("");
			supplyDealMm.setMmUnit("EA");
			supplyDealMm.setInqStockAddressCode("");
			supplyDealMm.setPlanGoodType("K");
			supplyDealMm.setPlanNedType(NeedTypeEnum.ZB6.getValue());
			if (supplyDealMm.getDealNum() == null || supplyDealMm.getDealNum().compareTo(BigDecimal.ZERO) == 0) {

			} else {
				supplyDealMm.setMmRowNum((++i) * 10 + "");
			}
			if (supplyDealMmMapper.update(supplyDealMm) == 0) {
				result += supplyDealMmMapper.insert(supplyDealMm);
			} else {
				result++;
			}
		}

		// 生成采购订单
//		if (StringUtils.isNullOrEmpty(orderVoModel.getOrdCode())) {
		orderVoModel.setOrdIsSign(Global.NO + "");
//			// 调用sap接口创建采购订单
		HttpResult sapHttpResult = sapPurchaseService.createSapOrder(supplyDeal, orderVoModel);
		if (sapHttpResult.getCode() == HttpStatus.SUCCESS_CODE) {
			orderVoModel.setOrdCode(sapHttpResult.getData().toString());
		} else {
			logger.error(sapHttpResult.getMsg());
			if (sapHttpResult.getMsg().contains("物料")) {
				return HttpResult.error("请联系主数据管理员：" + sapHttpResult.getMsg());
			} else {
				return HttpResult.error("请根据以下错误信息先查找使用手册常见问题：" + sapHttpResult.getMsg());
			}
		}
//		}

		if (orderVoModel.getStatus().equals(Global.YES + "")) {
			if (!StringUtils.isNotNull(sysUser.getSap())) {
				return HttpResult.error("采购组账号不存在！");
			}
		}
		orderVoModel.setOrdFreight(supplyDeal.getInfLastTotalPrice());
		orderVoModel.setResName(orderVoModel.getOrdName());
		orderVoModel.setOrdContractType(Global.YES + "");

		if (orderVoModel.getStatus().equals(Global.YES + "")) {
			// 调用oa接口
			HttpResult oaResult = specialContractService.CreateContract(orderVoModel, sysUser.getWxid());
			if (oaResult.getCode() != HttpStatus.SUCCESS_CODE) {
				// 强制手动事务回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return HttpResult.error(oaResult.getMsg());
			}
			OaWorkFlowResult oaWorkFlowResult = (OaWorkFlowResult) oaResult.getData();
			orderVoModel.setOaFlowId(oaWorkFlowResult.getOaFlowId());
		}
		if (orderDao.update(orderVoModel) == 0) {
			result += orderDao.insert(orderVoModel);
		}

		if (result > 0) {
			httpResult.setMsg(orderVoModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(orderVoModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(orderVoModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	@Autowired
	private SysUserMapper sysUserMapper;

	/**
	 * 根据采购结果生成采购订单
	 * 
	 * @param resId
	 * @return
	 * @throws ServiceException
	 * @throws RemoteException
	 */
	@Transactional
	public HttpResult createOrder(String resId) {
		if (!StringUtils.isNotNull(resId)) {
			return HttpResult.error("参数不能为空！");
		}
		//
		PurchaseInquiryResult inquiryResult = resultMapper.load(resId);
		if (inquiryResult == null) {
			return HttpResult.error("不存在此条询价结果!");
		}
		SysUser sysUser = sysUserMapper.selectByPrimaryKey(inquiryResult.getCreateId());
//		HashMap<String, Object> dicHashMap = new HashMap<String, Object>();
//		dicHashMap.put("dicName", DictGlobal.oaPurchaseCheckParentNameString);
//		dicHashMap.put("query", DictGlobal.oaPurchaseCheckNameString);
//		List<SysDict> dicts = sysDictService.queryItemByTypeName(dicHashMap);
//		if (dicts != null && dicts.size() > 0) {
//			String valueString = dicts.get(0).getDicValue();
//			BigDecimal bigDecimal = new BigDecimal(valueString);
//			if (bigDecimal.compareTo(inquiryResult.getResMoney()) <= 0) {
		if (getWFStatusService.getWFStatus(inquiryResult.getOaFlowId() + "", sysUser.getWxid())
				.getCode() != HttpStatus.SUCCESS_CODE) {
			return HttpResult.error("oa流程(" + inquiryResult.getOaFlowId() + ")未审批结束！");
		}
//			}
//		}
		int result = 0;
		inquiryResult.setStatus(Global.NUMERICAL_TWO + "");
		result += resultMapper.update(inquiryResult);
		// 根据供应商生成订单
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("resId", resId);
		List<HashMap<String, Object>> filterList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> filterMap = new HashMap<>();
		filterMap.put("field", "dealIsCheck");
		filterMap.put("value", "1");
		filterList.add(filterMap);
//		filterMap = new HashMap<>();
//		filterMap.put("field", "inqSapStatus");
//		filterMap.put("value", "1");
//		filterList.add(filterMap);
		hashMap.put("filter", filterList);
		List<PurchaseSupplyDeal> supplyDeals = supplyDealMapper.selectList(hashMap);
		for (PurchaseSupplyDeal supplyDeal : supplyDeals) {
			// 生成采购订单
			OrderModel orderModel = new OrderModel();
			orderModel.setOrdId(UUID.randomUUID().toString());

			orderModel.setResId(resId);
			orderModel.setInfId(supplyDeal.getInfId());
			orderModel.setInfCode(supplyDeal.getInfCode());
			orderModel.setInfName(supplyDeal.getInfName());
			orderModel.setOrdFreight(supplyDeal.getInfLastTotalPrice());
			orderModel.setResName(inquiryResult.getInqName());
			orderModel.setOrdName(inquiryResult.getInqName());
			orderModel.setResCode(inquiryResult.getResCode());
			orderModel.setOrdContractName(supplyDeal.getInfContact());
			result += orderDao.insert(orderModel);
		}
		HttpResult httpResult = new HttpResult();
		if (result > 0) {
			httpResult.setMsg("生成成功！");
		} else {
			httpResult.setMsg("生成失败！");
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String ordId) {
		if (!StringUtils.isNotNull(ordId)) {
			return HttpResult.error("参数不能为空");
		}
		OrderModel model = orderDao.load(ordId);
		if (!model.getStatus().equals(Global.NO + "")) {
			return HttpResult.error("非待提交状态，不可删除！");
		}
		HttpResult httpResult = new HttpResult();
		int result = orderDao.delete(ordId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	public HttpResult deleteSpecial(String ordId, String resId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(ordId)) {
			return HttpResult.error("参数不能为空");
		}
		OrderModel model = orderDao.load(ordId);
		if (!model.getStatus().equals(Global.NO + "")) {
			return HttpResult.error("非待提交状态，不可删除！");
		}
		int result = orderDao.delete(ordId);
		supplyDealMapper.deleteByResId(resId);
		supplyDealMmMapper.deleteByResId(resId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<OrderModel> list = orderDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), OrderModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
